<?php

namespace yak\layui\models;

use Yii;
use yii\behaviors\BlameableBehavior;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "lay_admin_config".
 *
 * @property int $id
 * @property string|null $theme
 * @property int|null $flexible
 */
class AdminConfig extends Yak
{
    public $_theme;

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'lay_admin_config';
    }

    public function behaviors()
    {
        return [
            [
                'class' => BlameableBehavior::class,
                'createdByAttribute' => 'theme',
                'updatedByAttribute' => 'theme',
                'value' => function () {
                    $configs = array_combine(array_column($this->theme(), 'alias'), $this->theme());
                    $config = ArrayHelper::getValue($configs, $this->_theme, []);
                    return $this->css($config);
                }
            ]
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['flexible'], 'integer'],
            [['_theme'], 'string'],
            ['id', 'unique']
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'theme' => 'Theme',
            'flexible' => 'Flexible',
        ];
    }

    public static function config()
    {
        return static::find()->where(['id' => Yii::$app->user->id])->asArray()->one();
    }

    public function themeLabel()
    {
        return ArrayHelper::map($this->theme(), 'alias', 'title');
    }

    public function theme()
    {
        return [
            [
                'main' => '#20222A', //主题色
                'selected' => '#009688', //选中色
                'alias' => 'default', //默认别名
                'title' => '默认',
            ],
            [
                'main' => '#03152A',
                'selected' => '#3B91FF',
                'alias' => 'dark-blue',
                'title' => '藏蓝',
            ],
            [
                'main' => '#2E241B',
                'selected' => '#A48566',
                'alias' => 'coffee',
                'title' => '咖啡',
            ],
            [
                'main' => '#50314F',
                'selected' => '#7A4D7B',
                'alias' => 'purple-red',
                'title' => '紫红',
            ],
            [
                'main' => '#344058',
                'logo' => '#1E9FFF',
                'selected' => '#1E9FFF',
                'alias' => 'ocean',
                'title' => '海洋',
            ],
            [
                'main' => '#3A3D49',
                'logo' => '#2F9688',
                'selected' => '#5FB878',
                'alias' => 'green',
                'title' => '墨绿',
            ],
            [
                'main' => '#20222A',
                'logo' => '#F78400',
                'selected' => '#F78400',
                'alias' => 'red',
                'title' => '橙色',
            ],
            [
                'main' => '#28333E',
                'logo' => '#AA3130',
                'selected' => '#AA3130',
                'alias' => 'fashion-red',
                'title' => '时尚红',
            ],
            [
                'main' => '#24262F',
                'logo' => '#3A3D49',
                'selected' => '#009688',
                'alias' => 'classic-black',
                'title' => '经典黑',
            ],
            [
                'logo' => '#226A62',
                'header' => '#2F9688',
                'alias' => 'green-header',
                'title' => '墨绿头',
            ],
            [
                'main' => '#344058',
                'logo' => '#0085E8',
                'selected' => '#1E9FFF',
                'header' => '#1E9FFF',
                'alias' => 'ocean-header',
                'title' => '海洋头',
            ],
            [
                'header' => '#393D49',
                'alias' => 'classic-black-header',
                'title' => '经典黑头',
            ]
        ];
    }

    public function css($config)
    {
        $tpl = [
            // 主题色
            '.layui-side-menu,'
            , '.layadmin-pagetabs .layui-tab-title li:after,'
            , '.layadmin-pagetabs .layui-tab-title li.layui-this:after,'
            , '.layui-layer-admin .layui-layer-title,'
            , '.layadmin-side-shrink .layui-side-menu .layui-nav>.layui-nav-item>.layui-nav-child'
            , '{background-color:{{mainColor}} !important;}'

            // 选中色
            , '.layui-nav-tree .layui-this,'
            , '.layui-nav-tree .layui-this>a,'
            , '.layui-nav-tree .layui-nav-child dd.layui-this,'
            , '.layui-nav-tree .layui-nav-child dd.layui-this a'
            , '{background-color:{{selectedColor}} !important;}'

            // logo
            , '.layui-layout-admin .layui-logo{background-color:{{logoColor}} !important;}'
        ];

        $headerTpl = [
            // 头部色
            '.layui-layout-admin .layui-header{background-color:{{headerColor}};}'
            , '.layui-layout-admin .layui-header a,'
            , '.layui-layout-admin .layui-header a cite{color: #f8f8f8;}'
            , '.layui-layout-admin .layui-header a:hover{color: #fff;}'
            , '.layui-layout-admin .layui-header .layui-nav .layui-nav-more{border-top-color: #fbfbfb;}'
            , '.layui-layout-admin .layui-header .layui-nav .layui-nav-mored{border-color: transparent; border-bottom-color: #fbfbfb;}'
            , '.layui-layout-admin .layui-header .layui-nav .layui-this:after, .layui-layout-admin .layui-header .layui-nav-bar{background-color: #fff; background-color: rgba(255,255,255,.5);}'
            , '.layadmin-pagetabs .layui-tab-title li:after{display: none;}'
        ];

        $mainColor = ArrayHelper::getValue($config, 'main', '#20222A');
        $selectedColor = ArrayHelper::getValue($config, 'selected', '#009688');
        $logoColor = ArrayHelper::getValue($config, 'logo', $mainColor);
        $headerColor = ArrayHelper::getValue($config, 'header');

        $main = implode(" ", $tpl);
        $header = $headerColor ? implode(" ", $headerTpl) : '';
        return strtr($main . $header, [
            '{{mainColor}}' => $mainColor,
            '{{selectedColor}}' => $selectedColor,
            '{{logoColor}}' => $logoColor,
            '{{headerColor}}' => $headerColor
        ]);
    }
}
